<?php namespace Home\Controller; 

use Hdphp\Cart\Cart;
/**
 * 列表控制器
 */
class CartController extends Cart {
	public function __construct()
	{
		//如果没有登陆
		if (!isset($_SESSION['hname'])||!isset($_SESSION['id'])) {
			View::success("没有登陆",'login/index');
		}
	}
	public function index()
	{
		$data=cart::getAllData(); 
		View::with('data',$data);
		View::make();
	}
	public function edit()
	{
		if(IS_AJAX){
			$data=Q('post.data');
			Cart::update($data);	
		}
	}
	public function delete()
	{
		$sid=Q('get.sid');
		$this->del($sid);
		View::success("删除成功",'index');
	}
	public function checkout()
	{
		$hudata=array();
		$model=new \Common\Model\Order;
		$humodel=new \Common\Model\Hurl;
		$listmodel=new \Common\Model\Orderlist;
		$orderid=Cart::getOrderId();
		if(!empty($_POST['huname'])){
			if ($humodel->store()) {
				View::success('添加成功');
			}
			$this->error($model->getError());
		}
		if(!empty($_POST['oname'])){
			$_POST['orderid']=$orderid;
			$oid=$model->store();
			p($_POST);
			foreach ($_POST['olnum'] as $k => $v) {
				$hudata[$k]['olnum']=$_POST['olnum'][$k];
				$hudata[$k]['goods_gid']=$_POST['goods_gid'][$k];
				$hudata[$k]['olprice']=$_POST['olprice'][$k];
				$hudata[$k]['order_oid']=$oid;
			}
			foreach ($hudata as $k => $v) {
				p($v);
				if(!$listmodel->store($v)) View::error($listmodel->getError());	
			}
			
			if ($oid) {
				go(U('pay',array('oid'=>$oid)));
			}
			$this->error($model->getError());
			
		}
		$data=cart::getAllData();
		View::with('data',$data);
		$hudata=$humodel->get();
		View::with('hudata',$hudata);
		View::make();
	}
	public function pay()
	{
		$model=new \Common\Model\Order;
		$oid=Q('get.oid',0,'intval');
		$data=$model->where("oid={$oid}")->find();
		View::with('data',$data);
		View::make();
	}
    
}
?>